<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span class="card-title">查询条件</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="showSearch = !showSearch">
          {{ showSearch ? '收起' : '展开' }}
        </el-button>
      </div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="姓名" prop="xm">
              <el-input
                v-model="queryParams.xm"
                placeholder="请输入姓名"
                clearable
                prefix-icon="el-icon-user"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="性别" prop="xb">
              <el-select v-model="queryParams.xb" placeholder="请选择性别" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="民族" prop="mz">
              <el-input
                v-model="queryParams.mz"
                placeholder="请输入民族"
                clearable
                prefix-icon="el-icon-office-building"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="政治面貌" prop="zzMm">
              <el-input
                v-model="queryParams.zzMm"
                placeholder="请输入政治面貌"
                clearable
                prefix-icon="el-icon-medal"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 操作按钮区域 -->
    <el-card class="box-card" shadow="hover" style="margin-top: 15px">
      <div class="table-operations">
        <el-button-group>
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['xsxx:jbxx:add']"
          >新增</el-button>
          <el-button
            type="success"
            icon="el-icon-edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['xsxx:jbxx:edit']"
          >修改</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['xsxx:jbxx:remove']"
          >删除</el-button>
          <el-button
            type="warning"
            icon="el-icon-download"
            @click="handleExport"
            v-hasPermi="['xsxx:jbxx:export']"
          >导出</el-button>
        </el-button-group>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>

      <!-- 表格区域 -->
      <el-table
        v-loading="loading"
        :data="jbxxList"
        @selection-change="handleSelectionChange"
        border
        stripe
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" width="80" align="center" />
        <el-table-column label="学生照片" align="center" prop="xsZp" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.xsZp" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="xm" width="120" />
        <el-table-column label="性别" align="center" prop="xb" width="80">
          <template slot-scope="scope">
            <el-tag :type="scope.row.xb === '1' ? 'primary' : 'success'" size="mini">
              <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.xb"/>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="出生日期" align="center" prop="scRq" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.scRq, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="民族" align="center" prop="mz" width="100" />
        <el-table-column label="政治面貌" align="center" prop="zzMm" width="120" />
        <el-table-column label="身份证号" align="center" prop="sfZh" width="180" />
        <el-table-column label="联系方式" align="center" prop="lxFs" width="120" />
        <el-table-column label="家庭住址" align="center" prop="jtZz" min-width="200" show-overflow-tooltip />
        <el-table-column label="操作" align="center" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['xsxx:jbxx:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['xsxx:jbxx:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="学生照片" prop="xsZp">
              <image-upload v-model="form.xsZp"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="xm">
              <el-input v-model="form.xm" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="性别" prop="xb">
              <el-select v-model="form.xb" placeholder="请选择性别" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="scRq">
              <el-date-picker
                v-model="form.scRq"
                type="date"
                placeholder="选择出生日期"
                style="width: 100%"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="民族" prop="mz">
              <el-input v-model="form.mz" placeholder="请输入民族" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="政治面貌" prop="zzMm">
              <el-input v-model="form.zzMm" placeholder="请输入政治面貌" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="身份证号" prop="sfZh">
              <el-input v-model="form.sfZh" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="lxFs">
              <el-input v-model="form.lxFs" placeholder="请输入联系方式" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="家庭住址" prop="jtZz">
          <el-input v-model="form.jtZz" type="textarea" :rows="3" placeholder="请输入家庭住址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.app-container {
  padding: 20px;

  .box-card {
    .card-title {
      font-size: 16px;
      font-weight: bold;
      color: #303133;
    }
  }

  .table-operations {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .el-button-group {
    .el-button {
      margin-right: 0;
    }
  }

  .el-table {
    margin: 15px 0;
  }

  .el-form {
    .el-form-item {
      margin-bottom: 22px;
    }
  }

  .el-dialog {
    .el-dialog__body {
      padding: 20px 30px;
    }
  }
}
</style>

<script>
import { listJbxx, getJbxx, delJbxx, addJbxx, updateJbxx } from "@/api/xsxx/jbxx"

export default {
  name: "Jbxx",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学籍信息表格数据
      jbxxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xm: null,
        xb: null,
        mz: null,
        zzMm: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xm: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        scRq: [
          { required: true, message: "出生日期不能为空", trigger: "blur" }
        ],
        mz: [
          { required: true, message: "民族不能为空", trigger: "blur" }
        ],
        zzMm: [
          { required: true, message: "政治面貌不能为空", trigger: "blur" }
        ],
        sfZh: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        lxFs: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询学籍信息列表 */
    getList() {
      this.loading = true
      listJbxx(this.queryParams).then(response => {
        this.jbxxList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        jbxxId: null,
        xsZp: null,
        xm: null,
        xb: null,
        scRq: null,
        mz: null,
        zzMm: null,
        sfZh: null,
        lxFs: null,
        jtZz: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jbxxId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加学籍信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const jbxxId = row.jbxxId || this.ids
      getJbxx(jbxxId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改学籍信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jbxxId != null) {
            updateJbxx(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addJbxx(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jbxxIds = row.jbxxId || this.ids
      this.$modal.confirm('是否确认删除学籍信息编号为"' + jbxxIds + '"的数据项？').then(function() {
        return delJbxx(jbxxIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xsxx/jbxx/export', {
        ...this.queryParams
      }, `jbxx_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
